package com.wt.admin.service.image.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wt.admin.code.image.ImageItem3200;
import com.wt.admin.domain.dto.image.ImageItemDTO;
import com.wt.admin.domain.entity.image.ImageItemEntity;
import com.wt.admin.domain.vo.image.ImageItemVO;
import com.wt.admin.mapper.image.ImageItemMapper;
import com.wt.admin.service.image.ImageItemService;
import com.wt.admin.util.AssertUtil;
import com.wt.admin.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("imageItem")
public class ImageItemServiceImpl extends ServiceImpl<ImageItemMapper, ImageItemEntity> implements ImageItemService{

    @Resource
    private ImageItemMapper imageItemMapper;

    @Override
    public int findByCountImageClassificationId(Integer id) {
        return imageItemMapper.findByCountImageClassificationId(id);
    }

    @Override
    public PageUtil.PageVO<ImageItemVO> imageList(PageUtil.PageDTO<ImageItemDTO> data) {
        return PageUtil.findPage(data, (page) -> imageItemMapper.imageList(page, data.getData()));
    }

    @Override
    public ImageItemVO editItemFeatures(ImageItemDTO data) {
        AssertUtil.objIsNull(data.getImageUrl(), ImageItem3200.CODE_3201);
        if(CollUtil.isNotEmpty(data.getFeaturesConfig())){
            Map<Integer, List<ImageItemEntity.FeaturesConfig>> collect =
                    data.getFeaturesConfig().stream().map(i -> BeanUtil.copyProperties(i, ImageItemEntity.FeaturesConfig.class))
                    .collect(Collectors.groupingBy(ImageItemEntity.FeaturesConfig::getTypeID));
            data.setFeaturesNum(collect.size());
        }
        updateById(data);
        return null;
    }

    @Transactional
    @Override
    public ImageItemVO delImageItem(List<String> data) {
        AssertUtil.List.isEmpty(data, ImageItem3200.CODE_3204);
        removeByIds(data);
        return null;
    }

    @Override
    public void saveBatchs(List<ImageItemEntity> list) {
        saveBatch(list);
    }

    @Override
    public void saveOne(ImageItemEntity entity) {
        save(entity);
    }

    @Override
    public List<ImageItemEntity> findByClassificationId(Integer id) {
        return imageItemMapper.listByClassificationId(id);
    }

    @Override
    public void updateBatch(List<ImageItemEntity> imageItemVOList) {
        updateBatchById(imageItemVOList);
    }


}
